import img from "@/assets/images/no-data-marsk.png";
import darkImg from "@/assets/images/dark/nodata_dark.png";

import darkLoading from "@/assets/images/loading.gif";
import loading from "@/assets/images/loading-black.gif";
import whiteLoading from "@/assets/images/loading-white.gif";
export function setCookie(name, value) {
  //两个参数，一个是cookie的名子，一个是值
  var Days = 1; //此 cookie 将被保存 1 天
  var exp = new Date(); //new Date("December 31, 9998");
  exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
  document.cookie =
    name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
export function getCookie(name) {
  //取cookies函数
  var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
  if (arr == null && name == "themeStyle") return "normal";
  else if (arr == null) return null;
  else return unescape(arr[2]) === "undefined" ? null : unescape(arr[2]);
}
//暂无数据显示界面
export function noData(document, fontSize, showTitle, msg, zindex = 9998) {
  var themeStyle = getCookie("themeStyle");
  var color = {
    normal: {
      bgcolor: "#fff",
      txtcolor: "#ccc",
      imgpath: img,
    },
    dark: {
      bgcolor: "#050E3C",
      txtcolor: "rgba(111,189,255,1)",
      imgpath: darkImg,
    },
  };
  var bgColor;
  var txtColor;
  var imgPath;
  if (themeStyle == "normal") {
    bgColor = color.normal.bgcolor;
    txtColor = color.normal.txtcolor;
    imgPath = color.normal.imgpath;
  } else if (themeStyle == "dark") {
    bgColor = color.dark.bgcolor;
    txtColor = color.dark.txtcolor;
    imgPath = color.dark.imgpath;
  }
  var docHeight = $(document).height();
  var danwei = parseFloat(getCookie("rem") || 1);

  // var docWidth = $(document).width();
  var pl = $(document).css("padding-left");
  var pt = $(document).css("padding-top");
  var noDataMarsk =
    "<div class='no-data-mask'><p>" + (msg || "暂无数据") + "</p></div>";
  $(document).css("position", "relative");
  $(document).append(noDataMarsk);
  var $noDataMarsk = $(document).find(".no-data-mask");
  var h = 30;
  if (showTitle > 30) {
    h = showTitle;
  }
  $noDataMarsk.css({
    position: "absolute",
    left: "0" + pl,
    top: showTitle ? h + parseFloat(pt) : pt,
    width: "100%",
    // height: showTitle ? parseFloat(docHeight) - h : docHeight,
    height: showTitle
      ? (parseFloat(docHeight) - h) / danwei + "rem"
      : parseFloat(docHeight) / danwei + "rem", // 高度自适应
    background: bgColor + " url(" + imgPath + ") center center no-repeat",
    "background-size": "110% 105%",
    "z-index": zindex,
    "border-radius": "5.6rem",
    overflow: "hidden",
  });
  $noDataMarsk.find("p").css({
    "font-size": fontSize + "rem",
    width: "100%",
    // "line-height": showTitle ? docHeight - h + "px" : docHeight + "px",
    "line-height": showTitle
      ? (parseFloat(docHeight) - h) / danwei + "rem"
      : parseFloat(docHeight) / danwei + "rem", // 高度自适应
    color: txtColor,
    "text-align": "center",
  });
}
export function valueLoading(fatherid, bgcolor, loadingimg, isshow) {
  var themeStyle = getCookie("themeStyle");
  var bgColor;
  var loadingImg;
  if (themeStyle == "normal") {
    bgColor = bgcolor[0];
    // loadingImg = darkLoading;
    loadingImg = loading;
    if (loadingimg && loadingimg[0] === "loading-white.gif") {
      loadingImg = whiteLoading;
    }
  } else if (themeStyle == "dark") {
    bgColor = bgcolor[1];
    // loadingImg = loading;
    loadingImg = darkLoading;
  }
  var docHeight = $(fatherid).height();
  $(fatherid).css("position", "relative");
  // var pl = $(fatherid).css("padding-left");
  // var pr = $(fatherid).css("padding-right");
  var pt = $(fatherid).css("padding-top");
  var pb = $(fatherid).css("padding-bottom");
  if (!$(fatherid).find(".loading-hide").length) {
    var dataLoading = "<div class='loading-hide'></div>";
    $(fatherid).append(dataLoading);
  }
  var $dataLoading = $(fatherid).find(".loading-hide");
  $dataLoading.css({
    position: "absolute",
    left: "0",
    top: "0",
    width: "100%",
    height: docHeight + pt + pb,
    background: bgColor + " url(" + loadingImg + ") center center no-repeat",
    "background-size":
      getClientType() == "PC" ? "90rem 26.8rem" : "60rem 18.1rem",
    "z-index": "9998",
    "border-radius": "5.6rem",
    display: isshow == "show" ? "block" : "none",
  });
}

// 获取clientType
export function getClientType() {
  return getCookie("clientType") || "PC";
}

// 获取themeStyle
export function getThemeStyle() {
  return getCookie("themeStyle") || "normal";
}
